package com.cspg.design.request;

import android.util.Log;

import com.cspg.design.common.Constant;
import com.cspg.design.http.IService;
import com.cspg.design.http.RetrofitUtil;
import com.cspg.design.model.LocalStorageModel;
import com.cspg.design.model.ProjectModel;
import com.cspg.design.presenter.GTaskPresenter;
import com.cspg.design.presenter.UploadFilePresenter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LocalStorage {

    private Call call;
    private UploadFilePresenter presenter;

    public LocalStorage(UploadFilePresenter presenter){

        this.presenter = presenter;

    }

    public void uploadFiles(List<File> files)
    {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        for(File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
            builder.addFormDataPart("file", file.getName(), requestBody);
        }
        MultipartBody multipartBody = builder.build(); //List<MultipartBody.Part> parts = builder.build().parts();
        // 执行请求
        call = new RetrofitUtil().createReq(IService.class).uploadFilesMultipartBody(multipartBody);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                List<LocalStorageModel> localStorageModel= (List<LocalStorageModel>) response.body();
                if (null == localStorageModel) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestSuccess(localStorageModel);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

    /**
     * 这种上传方式支持单张图片和多张图片，同时也支持上传除了图片类的其他文件
     *
     * @param files
     * @return
     */
    public void upload(List<File> files, String folderName) {
        MultipartBody multipartBody = filesToMultipartBody(files);
        String url = Constant.HOST + "api/localStorage/add" + folderName;
        // 执行请求
        Call<ResponseBody> call = new RetrofitUtil().createReq(IService.class).upload(url, multipartBody);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()) {
                        String ret = response.body().string();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("Upload error:", t.getMessage());
            }
        });
    }

    public MultipartBody filesToMultipartBody(List<File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("img", file.getName(), requestBody);
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }
    public List<MultipartBody> filesToMultipartBodys(List<File> files) {
        List<MultipartBody> multipartBodys= new ArrayList<>();
        for (File file : files) {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("file", file.getName(), requestBody);
            builder.setType(MultipartBody.FORM);
            MultipartBody multipartBody = builder.build();
            multipartBodys.add(multipartBody);
        }
        return multipartBodys;
    }

}
